package sgi.client;


import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

import javax.swing.DefaultListModel;
import javax.swing.DefaultRowSorter;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.SpringLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Comparator;
import java.util.Date;
import java.awt.Color;
import java.io.IOException;

import javax.swing.UIManager;
import javax.swing.JTable;

import sgi.client.SGIPurchaseImageController.Source;
import sgi.details_new.ImageDetails;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextPane;

public class SGICreateReportsUI extends SGIClientFrame {
	
	private static SGICreateReportsController createReportController;
	private JDatePickerImpl datePicker;
	private SpringLayout sl_datePicker;
	private SpringLayout springLayout;
	private JPanel contentPane;
	private JLabel lblMessage;
	
	public SGICreateReportsUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 532, 525);
		
		
		setTitle("SGI Generate Reports");
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane = new JPanel();
		contentPane.setLayout(gridBagLayout);
		setContentPane(contentPane);
		
//		UtilDateModel model = new UtilDateModel();
//		JDatePanelImpl datePanel = new JDatePanelImpl(model);
//		datePicker = new JDatePickerImpl(datePanel);
//		springLayout.putConstraint(SpringLayout.NORTH, datePicker.getJFormattedTextField(), 0, SpringLayout.NORTH, datePicker);
//		springLayout.putConstraint(SpringLayout.WEST, datePicker.getJFormattedTextField(), -90, SpringLayout.EAST, datePicker);
//		springLayout.putConstraint(SpringLayout.SOUTH, datePicker.getJFormattedTextField(), 23, SpringLayout.NORTH, datePicker);
//		springLayout.putConstraint(SpringLayout.EAST, datePicker.getJFormattedTextField(), -35, SpringLayout.EAST, datePicker);
//		sl_datePicker = (SpringLayout) datePicker.getLayout();
//		datePicker.setBounds(388, 23, 109, 31);
//		GridBagConstraints gbc_datePicker = new GridBagConstraints();
//		gbc_datePicker.insets = new Insets(0, 0, 5, 5);
//		gbc_datePicker.gridx = 2;
//		gbc_datePicker.gridy = 1;
//		contentPane.add(datePicker, gbc_datePicker);
		
		
		JButton btnCustomerComplaints = new JButton("Create customer complaints report");
		btnCustomerComplaints.setToolTipText("Generate monthly customer complaints report");
		GridBagConstraints gbc_btnCustomerComplaints = new GridBagConstraints();
		gbc_btnCustomerComplaints.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnCustomerComplaints.insets = new Insets(0, 0, 5, 5);
		gbc_btnCustomerComplaints.gridx = 1;
		gbc_btnCustomerComplaints.gridy = 3; 
		getContentPane().add(btnCustomerComplaints, gbc_btnCustomerComplaints);
		

		
		JButton btnDatabaseState = new JButton("Create database state report");
		btnDatabaseState.setToolTipText("Generate monthly database state report");
		GridBagConstraints gbc_btnDatabaseState = new GridBagConstraints();
		gbc_btnDatabaseState.insets = new Insets(0, 0, 5, 5);
		gbc_btnDatabaseState.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnDatabaseState.gridx = 1;
		gbc_btnDatabaseState.gridy = 5;
		getContentPane().add(btnDatabaseState, gbc_btnDatabaseState);
		
		
		
		JButton btnPackageUsage = new JButton("Create package usage report");
		btnPackageUsage.setToolTipText("Generate monthly image package usage report");
		GridBagConstraints gbc_btnPackageUsage = new GridBagConstraints();
		gbc_btnPackageUsage.insets = new Insets(0, 0, 5, 5);
		gbc_btnPackageUsage.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnPackageUsage.gridx = 1;
		gbc_btnPackageUsage.gridy = 7;
		getContentPane().add(btnPackageUsage, gbc_btnPackageUsage);
		
		lblMessage = new JLabel("Select a report to create");
		GridBagConstraints gbc_lblMessage = new GridBagConstraints();
		gbc_lblMessage.anchor = GridBagConstraints.WEST;
		gbc_lblMessage.insets = new Insets(0, 0, 5, 5);
		gbc_lblMessage.gridx = 1;
		gbc_lblMessage.gridy = 11;
		contentPane.add(lblMessage, gbc_lblMessage);
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SGICreateReportsController.terminate();
			}
		});
		GridBagConstraints gbc_btnBack = new GridBagConstraints();
		gbc_btnBack.anchor = GridBagConstraints.EAST;
		gbc_btnBack.insets = new Insets(0, 0, 5, 5);
		gbc_btnBack.gridx = 1;
		gbc_btnBack.gridy = 16;
		contentPane.add(btnBack, gbc_btnBack);
		
		JTextPane txtpnpleaseNotePicking = new JTextPane();
		txtpnpleaseNotePicking.setEnabled(false);
		txtpnpleaseNotePicking.setBackground(UIManager.getColor("Button.background"));
		txtpnpleaseNotePicking.setText("*Please note: Picking a certain day has no bearing on monthly reports");
		GridBagConstraints gbc_txtpnpleaseNotePicking = new GridBagConstraints();
		gbc_txtpnpleaseNotePicking.insets = new Insets(0, 0, 5, 5);
		gbc_txtpnpleaseNotePicking.fill = GridBagConstraints.BOTH;
		gbc_txtpnpleaseNotePicking.gridx = 1;
		gbc_txtpnpleaseNotePicking.gridy = 16;
		//contentPane.add(txtpnpleaseNotePicking, gbc_txtpnpleaseNotePicking);
		
		
		btnCustomerComplaints.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
//				Date date = (Date)datePicker.getModel().getValue();
//				if (date == null) {
//					lblMessage.setText("Invalid date!");
//				}
//				else;
				lblMessage.setText("Generating report...");
				createReportController.report(0);
				
			}
		});
		
		btnDatabaseState.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lblMessage.setText("Generating report...");
				createReportController.report(1);
			}
		});
		
		btnPackageUsage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lblMessage.setText("Generating report...");
				createReportController.report(2);
			}
		});
		
	}
	
	public void setMessage(String msg)
	{
		this.lblMessage.setText(msg);
	}

	public void setController(SGICreateReportsController createReport)
	{
		SGICreateReportsUI.createReportController = createReport;
	}
	
}